// 简单处理网络请求
const handleRequest = async (params, showConsole = false, isPost = false) => {
	if (showConsole) console.log('请求参数', params);

	let hideLoading = false;
	let hideMsg = false;

	if (!hideLoading) {
		uni.showLoading({
			title: '加载中',
			mask: true
		})
	}

	return new Promise((resolve, reject) => {
		let url = 'https://jianxia.mazhoudao.com/api/';
		let data = isPost ? params : {
			data: JSON.stringify(params)
		}

		uni.request({
			url: url,
			data: data,
			method: isPost ? 'POST' : 'GET',
			success: (res) => {
				if (!hideLoading) uni.hideLoading();
				if (res.statusCode !== 200) {
					let err = `[${res.statusCode}] 请求失败`;

					uni.showToast({
						title: err,
						icon: 'none'
					});
					reject(err);
				} else {
					if (showConsole) console.log('接口返回', res);
					resolve(res.data);
				}
			},
			fail: (err) => {
				if (!hideLoading) uni.hideLoading();
				uni.showToast({
					title: `[${res.statusCode}] 请求失败`,
					icon: 'none'
				});

				reject(err);
			}
		})
	})
}

export {
	handleRequest
}
